import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import location from "@src/store/location";
import { getCouponAllShopList, getCouponAvailableShopList } from "@src/api/coupon";

Component({
  options: {
    addGlobalClass: true
  },
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: location,
    fields: ['latitude', 'longitude'],
    actions: ['getLocation']
  }],
  data: {
    allShopCanUse: 0,
    listData: []
  },
  methods: {
    getListData() {
      console.log('allShopCanUse', this.data.allShopCanUse)
      if (this.data.latitude != null) {
        if (this.data.allShopCanUse === 1) {
          //ture为全部门店可用
          this.getAllShopList();
        } else {
          this.getAailableShopList();
        }
      } else {
        this.getLocation().then(res => {
          if (this.data.allShopCanUse === 1) {
            //ture为全部门店可用
            this.getAllShopList();
          } else {
            this.getAailableShopList();
          }
        }).catch(error => {
          if (this.data.allShopCanUse === 1) {
            //ture为全部门店可用
            this.getAllShopList();
          } else {
            this.getAailableShopList();
          }
        });
      }
    },
    getAllShopList() {
      /*暂停刷新*/
      wx.stopPullDownRefresh();
      let data = {
        latitude: this.data.latitude,
        longitude: this.data.longitude
      };
      getCouponAllShopList(data).then(res => {
        if (res.code === 0) {
          let list = res.data.shopList;
          this.setData({ listData: list });
          console.log('listData', this.data.listData)
        }
      })
    },
    getAailableShopList() {
      let data = {
        couponId: this.data.couponId,
        latitude: this.data.latitude,
        longitude: this.data.longitude
      };
      getCouponAvailableShopList(data).then(res => {
        if (res.code === 0) {
          let list = res.data;
          this.setData({ listData: list });
          console.log('listData', this.data.listData)
        }
      })
    },
    //打电话按钮
    callPhoneBtn(e) {
      if (e.currentTarget.dataset.phone) {
        wx.makePhoneCall({ phoneNumber: e.currentTarget.dataset.phone });
      }
    },
    //地图
    openMap(e) {
      let data = e.currentTarget.dataset.shop;
      wx.openLocation({
        latitude: data.latitude,
        longitude: data.longitude,
        name: data.name,
        address: data.address
      });
    },
    dialogShow(passData) {
      this.listData = [];
      console.log('passData', passData)
      //判断券的状态
      if (passData && passData.allShopCanUse) {
        this.setData({ allShopCanUse: passData.allShopCanUse, shopListDialog: true })
      }
      //券传递过来的id
      if (passData && passData.couponId) {
        this.setData({ couponId: passData.couponId, allShopCanUse: 0, shopListDialog: true })
      }
      this.getListData();
    },
    close() {
      this.setData({ shopListDialog: false, listData: [] })
    },
    handleStop() {
      return false;
    }
  }
})
